package com.yjd.comm.fundservice.util;

import com.alibaba.fastjson.JSON;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.funds.dto.FundChannelConfigDto;
import com.yjd.comm.fundservice.dto.FundChannelInfoDto;
import com.yjd.comm.fundservice.enums.FundChannelEnum;
import com.yjd.comm.fundservice.model.FundChannelModel;
import com.yjd.comm.fundservice.service.IFundChannelService;
import com.yjd.comm.org.model.OrganizationModel;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.system.service.ISysParamService;
import com.yjd.comm.system.util.SysParamUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServiceHolder;
import com.yjd.comm.util.StringUtil;
import com.yjd.comm.vo.Constant;
import org.apache.log4j.Logger;

/**
 * 资金渠道工具类
 * Created by yjd-wap on 2018/3/8.
 */
public class FundChannelUtil {
    private static Logger logger = Logger.getLogger(FundChannelUtil.class);

    private static  IOrganizationService getOrganizationService() {
        return ServiceHolder.getLocalService(IOrganizationService.class, "organizationService");
    }

    private static IFundChannelService getFundChannelService() {
        return ServiceHolder.getLocalService(IFundChannelService.class, "fundChannelService");
    }

    /**
     * 获取当前资金渠道及相关风控配置
     * @param org_id
     * @return
     * @throws Exception
     */
    public static FundChannelInfoDto getCurrFundChannel(Long org_id)throws Exception {
        FundChannelInfoDto fundChannelInfoDto = null;
        if (org_id != null) {
            OrganizationModel orgModel = getOrganizationService().getModelById(org_id, DbWREnums.WRITE);
            if (orgModel == null) {
                FrameUtil.throwBaseException1("您请求的机构不存在!");
            }

            FundChannelEnum fundChannelEnum = null;
            Integer v_fund_id = 0;
            if (orgModel.getFund_channel().intValue() > 0) {
                //机构对应的资金渠道大于0则用机构配置的资金渠道
                v_fund_id = orgModel.getFund_channel();
            } else {
                FundChannelConfigDto fundChannelConfig = StringUtil.getObject(SysParamUtil.getValue(Constant.SysParamKeyEnum.PF_CURR_FUND_CHANNEL), FundChannelConfigDto.class);
                v_fund_id = fundChannelConfig.getFund_channel();
            }
            FundChannelModel fundChannelModel = getFundChannelService().getModelById(v_fund_id, DbWREnums.WRITE);
            fundChannelEnum = FundChannelEnum.parse(v_fund_id);
            if(!StringUtil.isEmpty(fundChannelModel.getRisk_management_config())){
                fundChannelInfoDto = StringUtil.getObject(fundChannelModel.getRisk_management_config(), FundChannelInfoDto.class);
            }else {
                fundChannelInfoDto = new FundChannelInfoDto();
            }
            fundChannelInfoDto.setFundChannelEnum(fundChannelEnum);
        }

        return fundChannelInfoDto;
    }
}
